library(plotly)
library(dplyr)
library(lubridate)
# load time series data
df <- read.csv("https://github.com/hslu-ige-laes/edar/raw/master/sampleData/eboBookEleMeter.csv",
stringsAsFactors=FALSE,
sep =";")
# rename column names
colnames(df) <- c("timestamp", "meterValue")
df$timestamp <- parse_date_time(df$timestamp,
orders = "YmdHMS",
tz = "Europe/Zurich")
df$timestamp <- force_tz(df$timestamp, tzone = "UTC")
# uncomment to filter time range if necessary
#df <- df %>% filter(timestamp > "2015-03-01 00:00:00", timestamp < "2015-04-01 00:00:00")
# Fill missing values with NA
grid.df <- data.frame(timestamp = seq(min(df$timestamp, na.rm = TRUE),
max(df$timestamp, na.rm = TRUE),
by = "15 mins"))
df <- merge(df, grid.df, all = TRUE)
# convert steadily counting energy meter value from kWh to power in kW
df <- df %>%
mutate(value = (meterValue - lag(meterValue))*4) %>%
select(-meterValue) %>%
na.omit()
# remove negative values which occur beause of change summer/winter time
df <- df %>% filter(value >= 0)
# add metadata for later grouping and visualization purposes
df$x <- hour(df$timestamp) + minute(df$timestamp)/60 + second(df$timestamp) / 3600
df$weekday <- weekdays(df$timestamp)
df$weekday <- factor(df$weekday, c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday", "Sunday"))
df$day <- as.Date(df$timestamp, format = "%Y-%m-%d %H:%M:%S")
df <- df %>% mutate(value = ifelse(x == 0.00, NA, df$value))
# plot graph with all time series
rangeX <- seq(0,24,0.25)
maxValue <- max(df$value, na.rm = TRUE)*1.05
df %>%
highlight_key(~day) %>%
plot_ly(x=~x,
y=~value,
color=~weekday,
type="scatter",
mode="lines",
line = list(width = 1),
alpha = 0.15,
colors = "dodgerblue4",
text = ~day,
hovertemplate = paste("Time: ", format(df$timestamp, "%H:%M"),
"<br>Date: ", format(df$timestamp, "%Y-%m-%d"),
"<br>Value: %{y:.0f}")) %>%
# workaround with add_trace to have fixed y axis when selecting a dedicated day
add_trace(x = 0, y = 0, type = "scatter", showlegend = FALSE, opacity=0) %>%
add_trace(x = 24, y = maxValue, type = "scatter", showlegend = FALSE, opacity=0) %>%
layout(title = "Daily Profiles - Overlayed",
showlegend = TRUE,
xaxis = list(
title = "Hour of day",
range = rangeX,
tickvals = list(0, 3, 6, 9, 12, 15, 18, 21),
showline=TRUE
),
yaxis = list(
title = "Power (kW)",
range = c(0, maxValue)
)
) %>%
highlight(on = "plotly_hover",
off = "plotly_doubleclick",
color = "orange",
opacityDim = 1.0,
selected = attrs_selected(showlegend = FALSE)) %>% # this hides elements in the legend
plotly::config(modeBarButtons = list(list("toImage")), displaylogo = FALSE)